<style>
	.mar-10 {
		margin-left: 50px;
		margin-right: 10px;
	}

	#_luckyAdminLink,#_luckyAdminOpen{
		font-weight: bold;
	}
</style>

<fieldset class="cbi-section">
	<p id="_luckyAdminLink"></p>
</fieldset>


<style>
	.cbi-section {
	  border: 1px solid #ccc;
	  padding: 10px;
	}
	.cbi-section td:first-child {
	  font-weight: bold;
	}
  </style>

<fieldset class="cbi-section">
	<legend><%:Main Program Information%></legend>
	<table>
	  <tr>
		<td><%:Installation Status%></td>
		<td id="_luckyInstallStatus"><%:Collecting data...%></td>
	  </tr>
	  <tr>
		<td><%:Lucky Status%></td>
		<td id="_luckyStatus"><%:Collecting data...%></td>
	  </tr>
	  <tr>
		<td><%:Lucky Arch%></td>
		<td id="_luckyArch"><%:Collecting data...%></td>
	  </tr>
	  <tr>
		<td><%:Compilation Time%></td>
		<td id="_luckyCompilationTime"><%:Collecting data...%></td>
	  </tr>
	  <tr>
		<td><%:Lucky Version%></td>
		<td id="_luckyVersion"><%:Collecting data...%></td>
	  </tr>
	</table>
  </fieldset>
  
  <fieldset class="cbi-section">
	<legend><%:Admin Panel Information%></legend>
	<table>
	  <tr>
		<td><%:Admin Panel%></td>
		<td id="_luckyAdminOpen"><%:Collecting data...%></td>
	  </tr>
	  <tr>
		<td><%:Admin Panel Login Info%></td>
		<td id="_luckyLoginInfo"><%:Collecting data...%></td>
	  </tr>
	</table>
  </fieldset>




<script type="text/javascript">
	var daip = document.getElementById('_daip');
	var luckyInstallStatus = document.getElementById('_luckyInstallStatus')
	//var luckyAdminPanelURL = document.getElementById('_luckyAdminPanelURL')
	var luckyArch = document.getElementById('_luckyArch')
	var luckyStatus = document.getElementById('_luckyStatus')
	var luckyCompilationTime = document.getElementById('_luckyCompilationTime')
	var luckyVersion = document.getElementById('_luckyVersion')
	var luckyLoginInfo = document.getElementById('_luckyLoginInfo')
	var luckyAdminOpen = document.getElementById('_luckyAdminOpen')
	var luckyHttpPort = document.getElementById('_luckyHttpPort')
	var luckyAllowInternetaccess = document.getElementById('_luckyAllowInternetaccess')
	var luckySafeURL = document.getElementById('_luckySafeURL')
	var luckyAdminLink = document.getElementById('_luckyAdminLink')

	var luckyInstalled = false
	var adminHttpURL = ""


	FlushLuckyInfo()

	MotitorLuckyState()


	function flushLuckyStatus(status) {
		if (status) {
			luckyStatus.innerHTML = '<b style=color:green><%:The Lucky service is running.%></b>'
			luckyStatus.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
			luckyStatus.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Stop%>" onclick="return StopService()"/>'


			//luckyAdminOpen.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Open AdminURL%>" onclick="return OpenAdminURL(this,adminHttpURL)"/>' 
			luckyAdminOpen.innerHTML = '<a href="' + adminHttpURL + '"' + ' target="_blank">' + adminHttpURL + '</a> '

			luckyAdminLink.innerHTML = '<b><a href="' + adminHttpURL + '"' + ' target="_blank">' + adminHttpURL + '</a></b> '

		} else {


			if (luckyInstalled) {
				luckyStatus.innerHTML = '<b style=color:red><%:The Lucky service is not running.%></b>'
				luckyStatus.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
				luckyStatus.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Start%>" onclick="return StartService()"/>'
			} else {
				luckyStatus.innerHTML = '<b style=color:red><%:Not installed%></b>'
			}


			luckyAdminOpen.innerHTML = ''
			luckyAdminLink.innerHTML = ''
		}
	}

	function FlushLuckyInfo() {
		XHR.get('<%=url([[admin]], [[services]], [[lucky_info]])%>', null,
			function (x, info) {

				if (info) {
					luckyArch.innerHTML = '<b style=color:blue>' + info.luckyArch + '</b>'
					if (info.luckyInfo == "") {

						luckyInstalled = false
						//console.log("没有安装")
						luckyStatus.innerHTML = '<b style=color:red><%:Not installed%></b>'
						luckyInstallStatus.innerHTML = '<b style=color:red><%:Not installed%></b>'
						luckyHttpPort.innerHTML = '<b style=color:red><%:Not installed%></b>'
						luckyCompilationTime.innerHTML = '<b style=color:red><%:Not installed%></b>'

						luckyLoginInfo.innerHTML = '<b style=color:red><%:Not installed%></b>'
						luckyAdminOpen.innerHTML = '<b style=color:red><%:Not installed%></b>'
						luckyAllowInternetaccess.innerHTML = '<b style=color:red><%:Not installed%></b>'
						luckySafeURL.innerHTML = '<b style=color:red><%:Not installed%></b>'

						luckyVersion.innerHTML = '<b style=color:red><%:Not installed%></b>'
						luckyVersion.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
						luckyVersion.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:get latest version%>" onclick="return OpenGithub()"/>'
						luckyAdminLink.innerHTML = ''
						return
					}

					luckyInstalled = true




					luckyInstallStatus.innerHTML = '<b style=color:green><%:Installed%></b>'
					console.log("已经安装")

					var luckyInfo = JSON.parse(info.luckyInfo)

					luckyCompilationTime.innerHTML = "<b style=color:green>" + luckyInfo.Date + "</b>"
					luckyVersion.innerHTML = "<b style=color:green>" + luckyInfo.Version + "</b>"
					luckyVersion.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
					luckyVersion.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:get latest version%>" onclick="return OpenGithub()"/>'

					adminHttpURL = "http://" + window.location.hostname + ":" + info.LuckyBaseConfigure.AdminWebListenPort
					if (info.LuckyBaseConfigure.SafeURL != undefined) {
						adminHttpURL += info.LuckyBaseConfigure.SafeURL
					}








					luckyLoginInfo.innerHTML =
						"<b style=color:green><%:DefaultAuth%>:666" + "</b>"
					luckyLoginInfo.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
					luckyLoginInfo.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Reset%>" onclick="return ResetAuthInfo(this)"/>'


					luckyHttpPort.innerHTML = '<input disabled id="_luckyHttpPortInput" type="text" class="cbi-input-text"data-type="uinteger" style="width:30%" value="' + info.LuckyBaseConfigure.AdminWebListenPort + '"/>'
					luckyHttpPort.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
					luckyHttpPort.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Change%>" onclick="return SetNewHttpPort(this)"/>'

					luckySafeURL.innerHTML = '<input disabled id="_luckySafeURLInput" type="text" class="cbi-input-text"data-type="uinteger" style="width:30%" value="' + info.LuckyBaseConfigure.SafeURL + '"/>'
					luckySafeURL.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
					luckySafeURL.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Change%>" onclick="return SetNewSafeURL(this)"/>'

					if (info.LuckyBaseConfigure.AllowInternetaccess) {
						luckyAllowInternetaccess.innerHTML = '<b style=color:green><%:allow%></b>'
						luckyAllowInternetaccess.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
						luckyAllowInternetaccess.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Disable%>" onclick="return SwitchAllowInternetaccess(false)"/>'
					} else {
						luckyAllowInternetaccess.innerHTML = '<b style=color:red><%:not allow%></b>'
						luckyAllowInternetaccess.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
						luckyAllowInternetaccess.innerHTML += '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Enable%>" onclick="return SwitchAllowInternetaccess(true)"/>'
					}
				}
			}
		);
	}

	function SwitchAllowInternetaccess(b) {
		console.log("切换:" + b)

		if (b) {
			if (confirm("<%:Are you sure Enalbe Internetaccess?%>")) {
				SetLuckyConfig("switch_Internetaccess", "true")
			}

			return
		}

		if (confirm("<%:Are you sure Disable Internetaccess?%>")) {
			SetLuckyConfig("switch_Internetaccess", "false")
		}


	}

	var luckyPreState = false
	function MotitorLuckyState() {
		XHR.poll(3, '<%=url([[admin]], [[services]], [[lucky_status]])%>', null,
			function (x, data) {
				if (data) {
					if (luckyPreState != data.running) {
						FlushLuckyInfo()
						console.log("服务状态发生变化")
					}

					luckyPreState = data.running
					flushLuckyStatus(data.running)
				}
			}
		);
	}

	function OpenAdminURL(btn, url) {
		winOpen(url)


	}

	function SetNewHttpPort(btn) {
		var luckyHttpPortInput = document.getElementById('_luckyHttpPortInput')
		console.log(luckyHttpPortInput.value)

		var newPort = prompt("<%:NewHttpPort%>");
		if (newPort == null) {
			return
		}

		if (luckyHttpPortInput.value == newPort) {
			return
		}

		if (!isNumber(newPort)) {
			alert("<%:portValueError%>");
			return
		}

		var newPortValue = parseInt(newPort)
		if (newPortValue <= 0 || newPortValue > 65535) {
			alert("<%:portValueError%>");
			return
		}
		console.log("新端口:" + newPortValue)
		SetLuckyConfig("admin_http_port", newPort)
	}

	function SetNewSafeURL(btn) {
		var luckySafeURLInput = document.getElementById('_luckySafeURLInput')
		console.log("fuckURL:" + luckySafeURLInput.value)

		var newSafeURL = prompt("<%:NewHttpPort%>");
		if (newSafeURL == null) {
			return
		}

		if (newSafeURL == luckySafeURLInput.value) {
			return
		}


		SetLuckyConfig("admin_safe_url", newSafeURL)
	}

	function ResetAuthInfo(btn) {
		if (confirm("<%:Reset 666 as admin account and password?%>")) {
			SetLuckyConfig("reset_auth_info", "")
		}


	}
	function SetLuckyConfig(key, value) {
		XHR.get('<%=url([[admin]], [[services]], [[lucky_set_config]])%>', { key: key, value: value },
			function (x, res) {
				if (res) {
					//console.log(res)
					if (res.ret != undefined && res.ret == 0) {
						FlushLuckyInfo()
						alert("<%:update success%>")
						ControllLuckyService("restart")
						setTimeout('document.location.reload()',2000)
					} else {
						alert("<%:update failed%>")
					}
				}
			});
	}

	function ControllLuckyService(action) {
		console.log("ControllLuckyService:" + action)
		XHR.get('<%=url([[admin]], [[services]], [[lucky_service]])%>', { action: action },
			function (x) {

			});
	}


	function StopService() {
		if (confirm("<%:are you sure stop lucky service?%>")) {
			ControllLuckyService("stop")
		}

	}

	function StartService() {
		if (confirm("<%:are you sure start lucky service?%>")) {
			ControllLuckyService("start")
		}

	}

	function OpenGithub() {
		winOpen("https://url21.ctfile.com/d/44547821-55537427-a5525e?p=16601")
	}

	function winOpen(url) {
		var winOpen = window.open(url);
		if (winOpen == null || typeof (winOpen) == 'undefined') {
			window.location.href = url;
		}
	};


	function isNumber(val) {
		// negative or positive
		return /^[-]?[\.\d]+$/.test(val);
	}


</script>